﻿using IRepositoryBll;
using IRepositoryDal;
using RepositoryEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RepositoryBll
{
    /// <summary>
    /// 用户业务访问层
    /// </summary>
    public class UserInfoBll : IUserInfoBll
    {
        private IUserInfoDal _userInfoDal;

        public UserInfoBll(IUserInfoDal userInfoDal)
        {
            _userInfoDal = userInfoDal;
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public bool AddUserInfo(UserInfo userInfo)
        {
            //UserInfoDal userInfoDal = new UserInfoDal();
            return _userInfoDal.Add(userInfo);
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <returns></returns>
        public bool DeleteUserInfo(string userInfoId)
        {
            return _userInfoDal.Delete(userInfoId);
        }

        /// <summary>
        /// 根据id获取用户
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <returns></returns>
        public UserInfo GetUserInfo(string userInfoId)
        {
            return _userInfoDal.GetUserInfo(userInfoId);
        }

        /// <summary>
        /// 获取用户集合
        /// </summary>
        /// <returns></returns>
        public List<UserInfo> GetUserInfoList()
        {
            return _userInfoDal.GetUserInfosDb().Where(u => u.IsDelete == false).ToList();
        }

        /// <summary>
        /// 软删除用户
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <returns></returns>
        public bool SoftDeleteUserInfo(string userInfoId)
        {
            UserInfo userInfo = _userInfoDal.GetUserInfo(userInfoId);
            if (userInfo != null)
            {
                userInfo.IsDelete = true;
                userInfo.DeleteTime = DateTime.Now;
                return _userInfoDal.Update(userInfo);
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public bool UpdateUserInfo(UserInfo userInfo)
        {
            //return _userInfoDal.UpdateUserInfo(userInfo);
            return _userInfoDal.Update(userInfo);
        }

        /// <summary>
        /// 更新用户的用户名、姓名
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <param name="userName"></param>
        /// <param name="sex"></param>
        /// <returns></returns>
        public bool UpdateUserInfo(string userInfoId, string userName, int sex)
        {
            UserInfo userInfo = _userInfoDal.GetUserInfo(userInfoId);
            if (userInfo != null)
            {
                userInfo.UserName = userName;
                userInfo.Sex = sex;
                return _userInfoDal.Update(userInfo);
            }
            else
            {
                return false;
            }
        }
    }
}
